package com.github.tangmonkmeat;

import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;

/**
 * Description:
 *
 * @author zwl
 * @version 1.0
 * @date 2021/7/15 下午7:38
 */
public class 全排列排序 {

    public static void main(String[] args) {

        int[][] res = new 全排列排序().exportAllOrders(new int[]{1, 1, 3});

        //int[] arr = new int[]{1,2,3};
        //
        //new 全排列排序().swap(arr,0,1);
        //
        //System.out.println(arr);

        System.out.println("");
    }

    // 1,2,3

    int len;
    int n;
    int i = 0;

    public int[][] exportAllOrders(int[] array) {
        n = 1;
        for(int i = array.length;i > 0; i--){
            n = n * i;
        }
        len = array.length;
        int[][] res = new int[n][len];
        def(res,array,0);
        return res;
    }

    public void def(int[][] res,int[] array,int k){
        if (k == len){
            int[] t = new int[len];
            System.arraycopy(array,0,t,0,len);
            res[i++] = t;
        }
        for (int j = k; j < len;j++){
            swap(array,j,k);
            def(res,array,k + 1);
            swap(array,j,k);
        }
    }

    public void swap(int[] arr,int a,int b){
        if (a != b){
            arr[a] = arr[a] ^ arr[b];
            arr[b] = arr[a] ^ arr[b];
            arr[a] = arr[a] ^ arr[b];
        }
    }
}